package com.cheng.dataStructure.tree;

/**
 * @User Administrator
 * @Classname BinaryTreeDemo
 * @Project Data-structure-and-algorithm
 * @Description 二叉树的前序中序后序遍历
 * @Author wpcheng
 * @Create 2021-08-21-9:54
 * @Version 1.0
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {

        //创建二叉树
        BinaryTree binaryTree = new BinaryTree();
        //定义节点
        HeroNode root = new HeroNode(1, "刘备");
        HeroNode node2 = new HeroNode(2, "关羽");
        HeroNode node3 = new HeroNode(3, "张飞");
        HeroNode node4 = new HeroNode(4, "黄忠");
        HeroNode node5 = new HeroNode(5, "马超");
        HeroNode node6 = new HeroNode(6, "墨子");
        HeroNode node7 = new HeroNode(7, "干将");
        //把节点插入到树中
        root.setLeft(node2);
        root.setRight(node3);
        node2.setRight(node4);
        node3.setRight(node5);
        node2.setLeft(node6);
        node4.setLeft(node7);

        binaryTree.setRoot(root);
        System.out.println("前序遍历");
        binaryTree.preOrder();
        System.out.println("中序遍历");
        binaryTree.infixOrder();
        System.out.println("后序遍历");
        binaryTree.postOrder();


    }

}

//定义二叉树BinaryTree
class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    //前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("树为空");
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("树为空");
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("树为空");
        }
    }


}


//定义二叉树的节点
class HeroNode {
    private int id;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    //编写前序遍历方法
    public void preOrder() {
        //先输出父节点
        System.out.println(this);

        if (this.left != null) {
            this.left.preOrder();//以左节点为父节点开始递归
        }
        if (this.right != null) {
            this.right.preOrder();//以右节点为父节点开始递归
        }
    }

    //编写中序遍历方法
    public void infixOrder() {

        if (this.left != null) {
            this.left.preOrder();//以左节点为父节点开始递归
        }
        //输出父节点
        System.out.println(this);

        if (this.right != null) {
            this.right.preOrder();//以右节点为父节点开始递归
        }
    }

    //编写后序遍历方法
    public void postOrder() {

        if (this.left != null) {
            this.left.preOrder();//以左节点为父节点开始递归
        }

        if (this.right != null) {
            this.right.preOrder();//以右节点为父节点开始递归
        }
        //输出父节点
        System.out.println(this);
    }


}